<?php
/*
---------------------------------------------------------------------------
status/graphdata.php for SALAM Frontend

Copyright 2009, 2010 Jacob McEntire

    This file is part of SALAM.

    SALAM is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    SALAM is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with SALAM.  If not, see <http://www.gnu.org/licenses/>.
---------------------------------------------------------------------------
*/
require("../../library/frontend.php");
if (isset($_GET['hostid']) && isset($_GET['triggerid']) && isset($_GET['datanames']) && isset($_GET['datatypes'])) {
	$host_id = check_xss($_GET['hostid']);
	$trigger_id = check_xss($_GET['triggerid']);
	$data_names = check_xss($_GET['datanames']);
	$data_types = check_xss($_GET['datatypes']);
	$data_names = explode("|", $data_names);
	$data_types = explode("|", $data_types);
	$data_result = run_query("SELECT time, data FROM trigger_data WHERE host_id = '$host_id' AND trigger_id = '$trigger_id' AND state != 9 ORDER BY time ASC");
	if (mysql_num_rows($data_result) != NULL) {
		$data = mysql_fetch_array($data_result);
		while(list($time, $data) = mysql_fetch_array($data_result)) {
			if ($data != NULL) {
				$domain[] = $time;
				$data = explode("|", $data);
				foreach ($data as $key => $value) {
					$range[$key][] = $value;
				}
			}
		}
		foreach ($range as $key => $data) {
		echo 'triggerdata', $key, ' = {
		id: "triggerdata', $key, '",
		domainscale: 1000,
		domain: [';
		$firstrow = TRUE;
		foreach ($domain as $value) {
			if ($firstrow == TRUE) {
				echo $value;
				$firstrow = FALSE;
			}
			else
				echo ', ', $value;
		}
		echo '],
		range: [';
		$firstrow = TRUE;
		foreach ($data as $value) {
			if ($firstrow == TRUE) {
				if ($data_types[$key] == '%')
					echo '100';
				elseif (!is_numeric($value))
					echo 0;
				else
					echo $value;
				$firstrow = FALSE;
			}
			elseif (!is_numeric($value))
				echo ', 0';
			else
				echo ', ', $value;
		}
		echo '],
		label: "', $data_names[$key], '",
		axis: "', $data_types[$key], '"
		}
		';
		}
	}
}
?>